﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Collections;
using System.Web.Mvc;

namespace ConsumeSys
{
    /// <summary>
    /// 验证用户是否登录
    /// </summary>
    public class IsLoginAttribute : FilterAttribute, IAuthorizationFilter
    {
        /// <summary>
        /// 用户业务逻辑
        /// </summary>
        private BLL.UserBLL bll_UserBLL = new BLL.UserBLL();

        public void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext == null)
                throw new ArgumentNullException("filterContext");

            ActionResult result = null;
            if (result == null)
            {
                result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new
                { 
                    controller = "UserLogin",
                    action = "Login",
                    ToUrl = ((System.Web.HttpContextBase)filterContext.RequestContext.HttpContext).Request.Url.ToString()//当前页面地址
                }));
            }

            if (!Common.Globals.IsLoginUser())
            {
                filterContext.Result = result;
                return;
            }
            //登录用户的信息是否合法
            Model.User _user = bll_UserBLL.GetLoginUser();
            if (_user == null || _user.id <= 0)
            {
                filterContext.Result = result;
                return;
            }
        }
    }
}
